import 'package:equatable/equatable.dart';
import 'package:backend.purchases/domain/entities/yookassa_api/payments/yookassa_api_bank_card_info.dart';
import 'package:backend.purchases/infrastructure/dtos/yookassa_api/payments/succeeded_payment/yookassa_api_bank_card_type_dto.dart';

const kYookassaApiBankCardInfoLast4Key = 'last4';
const kYookassaApiBankCardInfoExpiryMonthKey = 'expiry_month';
const kYookassaApiBankCardInfoExpiryYearKey = 'expiry_year';
const kYookassaApiBankCardInfoCardTypeKey = 'card_type';

class YookassaApiBankCardInfoDto extends Equatable {
  const YookassaApiBankCardInfoDto._internal({
    required this.last4,
    required this.expirationMonth,
    required this.expirationYear,
    required this.cardTypeDto,
  });

  final String last4;
  final String expirationMonth;
  final String expirationYear;
  final YookassaApiBankCardTypeDto cardTypeDto;

  @override
  List<Object?> get props {
    return [
      last4,
      expirationMonth,
      expirationYear,
      cardTypeDto,
    ];
  }

  factory YookassaApiBankCardInfoDto.fromJson(Map<String, dynamic> json) {
    final cardTypeString = json[kYookassaApiBankCardInfoCardTypeKey] as String;
    final cardTypeDto = YookassaApiBankCardTypeDto.fromString(cardTypeString);

    return YookassaApiBankCardInfoDto._internal(
      last4: json[kYookassaApiBankCardInfoLast4Key],
      expirationMonth: json[kYookassaApiBankCardInfoExpiryMonthKey],
      expirationYear: json[kYookassaApiBankCardInfoExpiryYearKey],
      cardTypeDto: cardTypeDto,
    );
  }

  YookassaApiBankCardInfo toEntity() {
    return YookassaApiBankCardInfo(
      last4: last4,
      expirationMonth: expirationMonth,
      expirationYear: expirationYear,
      cardType: cardTypeDto.toEntity(),
    );
  }
}
